<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/12
 * Time: 18:15
 */
namespace Home\Controller;
use Think\Controller;
class AnquanController extends BaseController {
    public $post_array=array();
    //获取用户的openid
    public function getBaseInfo(){
        $action=$_GET['action'];
        $post_data=json_encode($_POST);
        $appid='wx3c484176a1ad105d';
        $redirect_uri=urlencode('http://dwc.gametu.com/think/index.php?g=Home&m=Anquan&a='.$action.'');
        $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state='.$post_data.'#wechat_redirect';
        //var_dump($url);
        header("location:".$url);
    }

    public function bangding(){
        $this->display();
    }
    public function add_post_bangding(){
        $appid = 'wx3c484176a1ad105d';
        $appsecret = 'b314e4774f61a3ac2d81a82e8d71d93f';
        $code=$_GET['code'];
        $post_data=json_decode($_GET['state']);
        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        $res = $this->http_curl($url, 'get', 'json');
        //var_dump($post_data);
        $openid=$res['openid'];
        //var_dump($openid);
       $weixing_user= M('wx_user')->where(array('openid'=>$openid))->field('user,openid')->find();
        if(!$weixing_user['openid']){
            $this->script('绑定失败！');
        }
       if($weixing_user['user']){
           $this->script('你已经绑定过账号!');
       }

        $row=M('player')->where(array('user'=>$post_data->user))->field('password,name,salt,id')->find();

        if($row){
            //var_dump(md5($post_data->password.$row['salt']));
            //var_dump($row['password']);
            if($row['password']==md5($post_data->password.$row['salt'])){
                $data=array(
                    'uid'=>$row['id'],
                    'name'=>$row['name'],
                    'user'=>$post_data->user
                );
                //var_dump($data);
              if(M('wx_user')->where(array('openid'=>$openid))->save($data)!==false){
                  $this->script('绑定成功!','Player/anquan');
              }  ;
            }else{
                $this->script('密码不正确');
            }
        }else{
            $this->script('用户名不正确!');
        }
    }

    //修改密码
    public function edit_pwd(){
        $appid = 'wx3c484176a1ad105d';
        $appsecret = 'b314e4774f61a3ac2d81a82e8d71d93f';
        $code=$_GET['code'];
        $post_data=json_decode($_GET['state']);
        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        $res = $this->http_curl($url, 'get', 'json');
        //var_dump($post_data);
        $openid=$res['openid'];
        $weixing_user= M('wx_user')->where(array('openid'=>$openid))->field('user,openid')->find();
        if(!isset($weixing_user['user'])){
            $this->script('你还没绑定账号,不能修改密码!','Anquan/bangding');
        }
        $this->assign('user',$weixing_user['user']);
        $this->display();
    }


    //修改密码
    public function add_post_edit_pws(){
        $appid = 'wx3c484176a1ad105d';
        $appsecret = 'b314e4774f61a3ac2d81a82e8d71d93f';
        $code=$_GET['code'];
        $post_data=json_decode($_GET['state']);

        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        $res = $this->http_curl($url, 'get', 'json');
        //var_dump($post_data);
        $openid=$res['openid'];
        $row=M('player')->where(array('user'=>$post_data->user))->field('password,name,salt,id')->find();

        if($row){
            $new_password=$post_data->new_password;
            $new_two_password=$post_data->new_two_password;

            if(strlen($new_password)<6 || strlen($new_password)>12){
                $this->script('密码长度必须是6到12位');
            }
            if($new_password!=$new_two_password){
                $this->script('两次输入的密码不相同');
            }


            if($row['password']==md5($post_data->password.$row['salt'])){

                $new_pas=md5($post_data->new_password.$row['salt']);

                //var_dump($data);
                if(M('player')->where(array('id'=>$row['id']))->save(array('password'=>$new_pas))!==false){

                    $this->script('修改密码成功!','player/anquan');
                }else{
                    $this->script('修改密码失败!');
                }  ;
            }else{
                $this->script('密码不正确');
            }
        }else{
            $this->script('账号名称不正确!');
        }
    }


    //解除绑定
    public function bangding_jc(){
        $appid = 'wx3c484176a1ad105d';
        $appsecret = 'b314e4774f61a3ac2d81a82e8d71d93f';
        $code=$_GET['code'];
        $post_data=json_decode($_GET['state']);
        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        $res = $this->http_curl($url, 'get', 'json');
        //var_dump($post_data);
        $openid=$res['openid'];
        $weixing_user= M('wx_user')->where(array('openid'=>$openid))->field('user,openid')->find();

        if(!isset($weixing_user['user'])||empty($weixing_user['user'])){
            $this->script('你还没绑定账号!','Player/anquan');
        }
        $this->assign('user',$weixing_user['user']);
        $this->display();
    }
    public function add_post_bangding_jc(){
        $appid = 'wx3c484176a1ad105d';
        $appsecret = 'b314e4774f61a3ac2d81a82e8d71d93f';
        $code=$_GET['code'];
        $post_data=json_decode($_GET['state']);
        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
        $res = $this->http_curl($url, 'get', 'json');
        //var_dump($post_data);
        $openid=$res['openid'];
        //var_dump($openid);
        $weixing_user= M('wx_user')->where(array('openid'=>$openid))->field('user,openid')->find();

        if(!isset($weixing_user['user'])|| empty($weixing_user['user'])){
            $this->script('你还没有绑定账号!');
        }

        $row=M('player')->where(array('user'=>$post_data->user))->field('password,name,salt,id')->find();
        //var_dump($row);
        if($row){
            //var_dump(md5($post_data->password.$row['salt']));
            //var_dump($row['password']);
            if($row['password']==md5($post_data->password.$row['salt'])){
                $data=array(
                    'uid'=>null,
                    'name'=>null,
                    'user'=>null
                );
                //var_dump($data);
                if(M('wx_user')->where(array('openid'=>$openid))->save($data)!==false){
                    $this->script('解除绑定成功!','Player/anquan');
                }  ;
            }else{
                $this->script('密码不正确');
            }
        }else{
            $this->script('用户名不正确!');
        }
    }
}